package com.ssuu.ssuusnow.project.manage.log.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ssuu.ssuusnow.project.manage.log.entity.Log;
import com.ssuu.ssuusnow.project.manage.log.mapper.LogMapper;
import com.ssuu.ssuusnow.project.manage.log.service.ILogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssuu.ssuusnow.project.manage.user.entity.User;
import com.ssuu.ssuusnow.utils.security.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sun
 * @since 2023-06-14
 */
@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, Log> implements ILogService {
    @Resource
    LogMapper logMapper;

    @Override
    public Object listTo(Log t) {
        QueryWrapper<Log> wrappers = new QueryWrapper<>();
//        wrappers.orderByDesc("createTime");
        wrappers.and(wrapper -> {
            wrapper.eq("isdel", false);
            wrapper.like("manner", Objects.isNull(t.getManner()) ? "" : t.getManner());
            wrapper.like("description", Objects.isNull(t.getDescription()) ? "" : t.getDescription());
            if (!Objects.isNull(t.getEnterpriseId())) {
                wrapper.eq("enterpriseId", t.getEnterpriseId());
            }});
        Page<Log> page = new Page<>(t.getCurrentPage(),t.getPageSize());
        Object userPage = t.getPageSize() > 0 && t.getCurrentPage() > 0 ? logMapper.selectPage(page, wrappers) : logMapper.selectList(wrappers);
        return userPage;
    }

    @Override
    public int AddTo(Log t) {
        t.setCreateTime(LocalDateTime.now());
        t.setUpdateTime(LocalDateTime.now());
        User lou =  SecurityUtils.LoginUsers();
        t.setUserid(lou.getId());
        t.setEnterpriseId(lou.getEnterpriseId());
        return logMapper.insert(t);
    }

    @Override
    public int UpdateTo(Log t) {
        t.setUpdateTime(LocalDateTime.now());
        User lou =  SecurityUtils.LoginUsers();
        t.setUserid(lou.getId());
        return logMapper.updateById(t);
    }

    @Override
    public int DelTo(Long t) {
        Log u = new Log();
        u.setId(t);
        u.setIsdel(true);
        u.setUpdateTime(LocalDateTime.now());
        User lou =  SecurityUtils.LoginUsers();
        u.setUserid(lou.getId());
        return logMapper.updateById(u);
    }
}
